using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_SupportedFacManagement : admin_BaseUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ActiveAccording = "facility";
        if (Request["mode"] == "det")
        {
            pnlDetails.Visible = true;
            pnlMain.Visible = false;
            LoadLocation();
            CurrentID = Request["fac_id"];
            int facId = 0;
            if(Request["fac_Id"]!=null)facId= int.Parse(Request["fac_id"]);
            FiltData(facId);
        }
        else
        {
            pnlDetails.Visible = false;
            pnlMain.Visible = true;
            LoadType();           
        }
       
    }
    public void LoadType()
    {
        FacilityTypeDAO daoFacType = new FacilityTypeDAO();     
            cmbFacType.DataSource = daoFacType.GetAllFacilityTypes("%");
            cmbFacType.DataTextField = "Name";
            cmbFacType.DataValueField = "Id";
            cmbFacType.DataBind();          
           cmbFacType.SelectedIndex = 0;
   
    }
    public void LoadLocation()
    {
        
        FacilityDAO daoFac = new FacilityDAO();
        LocationDAO daoLocation = new LocationDAO();        
        int total = 0;
        int typeId = 0;
        if (cmbFacType.SelectedIndex >= 0) typeId = int.Parse(cmbFacType.SelectedValue);
        cmbFacility.DataSource = daoFac.GetFacilitiesAdv(1, 100, ref total, "%", typeId, 0, 9999999, ServiceObject.Both);
        cmbFacility.DataTextField = "Name";
        cmbFacility.DataValueField = "Id";
        cmbFacility.DataBind();

        cmbFirstLoc.DataSource = daoLocation.GetAllLocations();
        cmbFirstLoc.DataTextField = "Name";
        cmbFirstLoc.DataValueField = "Id";
        cmbFirstLoc.DataBind();
        cmbSecondLoc.DataSource = daoLocation.GetAllLocations();
        cmbSecondLoc.DataTextField = "Name";
        cmbSecondLoc.DataValueField = "Id";
        cmbSecondLoc.DataBind();
    }
    /// <summary>
    /// Load data about all place relative to selected facility into gridview
    /// </summary>
    /// <param name="facId"></param>
    public void FiltData(int facId)
    {
        SupportedFacDAO daoSF = new SupportedFacDAO();
        dgrSupportedFac.DataSource = daoSF.GetSupportedPlaces(facId);
        dgrSupportedFac.DataBind();
    }
    protected void cmbFacType_SelectedIndexChanged(object sender, EventArgs e)
    {
        FacilityDAO daoFac = new FacilityDAO();
        int total = 0;
        int typeId = 0;
        if (cmbFacType.SelectedIndex >= 0) typeId = int.Parse(cmbFacType.SelectedValue);
        //assume that there is less than 100 record for a facility
        cmbFacility.DataSource = daoFac.GetFacilitiesAdv(1, 100, ref total, "%", typeId, 0, 9999999, ServiceObject.Both);
        cmbFacility.DataTextField = "Name";
        cmbFacility.DataValueField = "Id";
        cmbFacility.DataBind();
       
    }
    protected void lnkAdd_Click(object sender, EventArgs e)
    {
        //if (cmbFacility.SelectedIndex >= 0)

        SupportedFacDAO sfd = new SupportedFacDAO();
        SupportedFacility sf = new SupportedFacility();
        sf.Facility.Id = int.Parse(CurrentID);
        sf.DepartureLocation.Id = int.Parse(cmbFirstLoc.SelectedValue);
        sf.ArrivalLocation.Id = int.Parse(cmbSecondLoc.SelectedValue);
        if (sfd.Insert(sf) > 0)
        {
            ShowMessage("Information is added");
        }
        else ShowMessage("Error");

        int facId = 0;
        if (Request["fac_id"] != null) facId = int.Parse(Request["fac_id"]);
        FiltData(facId);
    }
    /// <summary>
    /// Delete a record
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dgrSupportedFac_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SupportedFacDAO daoSF=new SupportedFacDAO();
        int id = int.Parse(dgrSupportedFac.DataKeys[e.RowIndex].Value.ToString());
        if (daoSF.Delete(id) > 0)
        {
            ShowMessage("The information is deleted");
        }
        else ShowMessage("Error occured. Action failed.");
        int facId = 0;
        if (Request["fac_id"]!=null) facId = int.Parse(Request["fac_id"]);
        FiltData(facId);
    }
    protected void lnkFilt_Click(object sender, EventArgs e)
    {
        int facId = 0;
        if (cmbFacility.SelectedIndex >= 0) facId = int.Parse(cmbFacility.SelectedValue);
        FiltData(facId);       
    }
    protected void cmbFacility_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cmbFacility.SelectedIndex >= 0)
        {
            int facId = int.Parse(cmbFacility.SelectedValue);
            FiltData(facId);          
        }
       
    }
    
}
